﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MemoryManagementExercises;


public class ResourcePool
{
    private List<Resource> pool;

    private int capacity;

    public int Capacity
    {
        get { return capacity; }
    }
   
    public ResourcePool()
    {
       this.pool = new List<Resource>();
       this.capacity = 0;
    }

    public void InsureCapacity(int capacity)
    {
        while (pool.Count < capacity)
        {
            Resource res = Resource.GetResource();
            AddResource(res);
        }
    }

    private void AddResource(Resource resource)
    {
        pool.Add(resource);
        this.capacity++;
    }

    public Resource GetResource()
    {
        foreach (Resource res in pool)
        {
            if (res.Available)
            {
                res.Available = false;
                return res;
            }
        }
        int oldResources = pool.Count;
        InsureCapacity(2*oldResources);
        return pool.ElementAt(oldResources);
    }

    public override string ToString()
    {
        StringBuilder res = new StringBuilder();
        foreach (Resource resource in pool)
        {
            res.Append(String.Format("[{0}: {1} {2}] ", resource, resource.Info, resource.Available));
        }
      return res.ToString();
    }
}

